package org.testfire.translator.watij;

import org.testfire.model.ElementActionType;
import org.testfire.model.HtmlFinderType;
import org.testfire.model.TestsetType;
import org.testfire.translator.TestFireTranslator;
import org.testfire.translator.TranslatorFactory;

/**
 * Created by IntelliJ IDEA.
 * User: granum
 * Date: Sep 2, 2007
 * Time: 3:52:34 PM
 *
 */
public class ElementActionToWatijTranslator<T> implements TestFireTranslator<ElementActionType>
{
    

    /**
     * Will return a syntactically correct Java class, ready to be written out to a java file with the name
     * testset.getName() + ".java" and compiled.
     * @return The source code for a valid java class, containing all test cases.
     */
    public Object translate( TestsetType testset, ElementActionType ea )
    {
       StringBuilder buf = new StringBuilder();

        String key = ea.getKey();
        if(key != null && key.length() > 0)
        {
            testset.registerVariableKey( key, ea.getKeyType() );
            buf.append(ea.getKey()).append( " = " );
        }

        if ( ea.getUseObjectKey() != null && ea.getUseObjectKey().length() > 0 )
        {
            buf.append( ea.getUseObjectKey());
        }
        else
        {
            buf.append( "engine");
        }

        buf.append( "." );

        buf.append( TranslatorFactory.get( HtmlFinderType.class ).translate( testset, ea.getHtmlFinderType()) );
        return buf.toString();
    }
}